package com.tools.cleanmaster.manager;

import android.text.format.Formatter;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.controller.ITrashScanListener;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.store.TrashCloudSharedPrefManager;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.util.concurrent.TimeUnit;

/**
 * Created by lianzhan on 16/2/25.
 */
public class BackScannerManager {
    private static final String TAG = "BackScannerManager";
    private static BackScannerManager sInstance = null;

    private BackScannerManager() {

    }

    public static BackScannerManager getInstance() {
        if (sInstance == null) {
            synchronized(BackScannerManager.class) {
                if (sInstance == null) {
                    sInstance = new BackScannerManager();
                }
            }
        }
        return sInstance;
    }

    public void scan() {
        long currentTime = System.currentTimeMillis();
        long lastScanTime = TrashCloudSharedPrefManager.getBackScannerTime();
        if (currentTime - lastScanTime > TimeUnit.DAYS.toMillis(3)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    TrashCloudSharedPrefManager.setBackScannerTime(System.currentTimeMillis());
                    TrashManager.getInstance().scan(new ITrashScanListener() {
                        @Override
                        public void onScanStart() {

                        }

                        @Override
                        public void onScanProgressUpdate(String desc, int percent, long size) {

                        }

                        @Override
                        public void onScanTypeFinish(TrashType type, long size) {
                            if (FeatureConfig.DEBUG_LOG) {
                                LogHelper.i(TAG, "后台扫描 type=" + type + " 完成， size=" + Formatter.formatFileSize(CleanApplication.getInstance(), size));
                            }
                        }

                        @Override
                        public void onScanFinish() {
                            if (FeatureConfig.DEBUG_LOG) {
                                LogHelper.i(TAG, "后台扫描完成");
                            }
                        }

                        @Override
                        public void onScanStop() {

                        }
                    }, false, true);
                }
            }).start();

            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(TAG, "超过3天，开始后台扫描");
            }
            return;
        }
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(TAG, "和上次扫描未超过3天，不进行后台扫描");
        }
    }
}
